package vip.specimen.system.controller.config;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.datasource.entity.SystemApiConfigEntity;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemApiConfigService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.UUID;

/**
 * API接口CRUD
 * <p>
 * *@Author: 余浪人
 * *@Date：2021/2/19 15:54
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "配置管理")
@RestController
@RequestMapping(value = "/config/api")
public class ApiController {
    @Resource
    SystemApiConfigService apiService;

    @GetMapping
    @RequiresPermissions(value = "config:api:select")
    @ApiOperation(value = "查看Api密钥列表", notes = "获取Api密钥列表")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看Api密钥列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult<Page<SystemApiConfigEntity>> get() {
        return JsonResult.success(this.apiService.findAll());
    }

    @GetMapping(value = "{uuid}")
    @RequiresPermissions(value = "config:api:select")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看Api详情")
    @ApiOperation(value = "查看Api详情", notes = "根据uuid获取Api详情")
    public JsonResult<SystemApiConfigEntity> getOne(@PathVariable UUID uuid) {
        SystemApiConfigEntity api = this.apiService.findOne(uuid);
        return null != api ? JsonResult.success(api) : JsonResult.failed(ResultCode.ERROR_NOT_FOUND);
    }

    @PutMapping
    @RequiresPermissions(value = "config:api:insert")
    @SystemJurisdiction(resourceType = "button", permissionName = "添加Api接口")
    @ApiOperation(value = "新增Api", notes = "添加Api密钥")
    public JsonResult<SystemApiConfigEntity> insert() {
        SystemApiConfigEntity api = this.apiService.insert();
        return null != api ? JsonResult.success(api) : JsonResult.failed(ResultCode.ERROR_SQL_CREATE_SERVER);
    }

    @PostMapping
    @RequiresPermissions(value = "config:api:update")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新Api接口")
    @ApiOperation(value = "更新Api密钥", notes = "根据uuid更新Api密钥")
    public JsonResult<SystemApiConfigEntity> updateKey(@RequestBody HashMap<String, UUID> param) {
        SystemApiConfigEntity api = this.apiService.update(param.get("uuid"));
        return null != api ? JsonResult.success(api) : JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER);
    }

    /**
     * 启用-停用   切换
     *
     * @return 数据
     */
    @PostMapping(value = "enable")
    @RequiresPermissions(value = "config:api:update:enable")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新Api接口状态")
    @ApiOperation(value = "更新Api状态", notes = "根据uuid更新Api密钥状态")
    public JsonResult<SystemApiConfigEntity> updateEnable(@RequestBody HashMap<String, UUID> param) {
        SystemApiConfigEntity api = this.apiService.updateEnable(param.get("uuid"));
        return null != api ? JsonResult.success(api) : JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER);
    }

    @DeleteMapping
    @RequiresPermissions(value = "config:api:delete")
    @SystemJurisdiction(resourceType = "button", permissionName = "删除Api接口")
    @ApiOperation(value = "删除Api", notes = "根据uuid删除Api密钥")
    public JsonResult delete(@RequestBody HashMap<String, UUID> param) {
        return this.apiService.delete(param.get("uuid")) ? JsonResult.success() : JsonResult.failed(ResultCode.ERROR_DELETE);
    }
}
